package org.Shiikong_Memory.SakuraBlockFrame.AutoPluginConfig;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Set;

@SupportedAnnotationTypes("*") // 支持所有注解，仅用于触发处理
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class LangFileAutoCreator extends AbstractProcessor {
    private static final String LANG_FILE_NAME = "lang-cn.yml";

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        System.out.println("开始检查并创建 lang-cn.yml 文件");
        try {
            // 尝试创建 lang-cn.yml 文件，如果文件已存在会抛出异常
            FileObject fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", LANG_FILE_NAME);
            try (Writer writer = new OutputStreamWriter(fileObject.openOutputStream(), StandardCharsets.UTF_8)) {
                writer.write("# 中文语言配置文件\n");
            }
            System.out.println("lang-cn.yml 文件创建成功");
        } catch (IOException e) {
            System.out.println("lang-cn.yml 文件已存在，跳过创建");
        }
        return false; // 不处理任何注解，返回 false
    }
}
